//index.js
//获取应用实例
var cfg = require('../../utils/cfg');
var util = require('../../utils/util');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
var common = require('../../utils/common');
var sendRequest = require('../../utils/requestUtils');

Page({
  data: {
    mask1Hidden : true,
    areas : [],
    selArea : {},
    selAreaId : -1,
    merchants : []
  },
  onLoad: function () {
    let that = this;
    that.loadAreas();
  },

  loadMerchants : function(area) {
    let that = this;
    sendRequest.sendReq(cfg.djurl.listMerchant, {
      areaId: area.id
    }, function (result) {
      console.log('>>>>>>listMerchant=' + result.data.length);
      if (result.data.length == 0) {
        util.alert('暂无商家信息');
        that.setData({
          merchants: [],
          mask1Hidden: true
        });
        return;
      }

      that.setData({
        merchants: result.data,
        mask1Hidden: true
      });
    });
  },

  loadAreas : function() {
    let that = this;
    sendRequest.sendReq(cfg.djurl.listAreas, {
    }, function(result) {
      console.log('>>>>>>areas=' + result.data.length);
      let selArea = {};
      if (result.data.length > 0) {
        selArea = result.data[0];
      } else {
        util.alert('暂无数据');
        return;
      }

      that.setData({
        areas: result.data,
        selArea: selArea
      });

      //加载第一个区域的商家
      that.loadMerchants(selArea);

      let timerTem = setTimeout(function () {
        //加载离我最近的区域
        that.loadMyArea();
      }, 1000)

    });
  },

  loadMyArea : function() {
    let that = this;
    //获取坐标
    util.getCoordinate(function (lon, lat) {
      console.log('lon=' + lon + ' lat=' + lat)

      sendRequest.sendReq(cfg.djurl.myArea, {
        lon: lon,
        lat: lat
      }, function (result) {
        let myArea = result.data;
        if (myArea.areaName == '') {
          return;
        }

        if (myArea.id == that.data.selArea.id) {
          console.log('区域命中');
          return;
        }

        that.setData({
          selArea: myArea
        });

        that.loadMerchants(myArea);
      });
    });
  },

  showAreasHandler : function() {
    let that = this;

    // this.data.selAreaId = result.detail.id;
    that.setData({
      mask1Hidden: false
    })
  },
  areaSelected: function (event) {
    var that = this;
    let selAreaId = event.currentTarget.dataset.areaId
    let selArea = {};

    console.log('selAreaId=' + selAreaId);

    for (let i = 0; i < that.data.areas.length; i++) {
      if (that.data.areas[i].id == selAreaId) {
        selArea = that.data.areas[i];
        break;
      }
    }
  
    this.setData({
      mask1Hidden: true,
      selArea: selArea,
      selAreaId: selAreaId
    })

    //加载商家
    that.loadMerchants(selArea);
  },

  toMerchantDetail : function(event) {
    let that = this;

    let merchantId = event.currentTarget.dataset.merchantId
    console.log('merchantId=' + merchantId);

    let merchant = undefined;
    for (let i = 0; i < that.data.merchants.length; i++) {
      if (that.data.merchants[i].id == merchantId) {
        // wx.setStorageSync('merchant', that.data.merchants[i]);
        merchant = that.data.merchants[i];
        break;
      }
    }

    if (merchant) {
      wx.navigateTo({
        url: '/pages/caidan/caidan?merchant=' + JSON.stringify(merchant)
      })
    }
  }

})
